package com.tangkeyon.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.tangkeyon.common.ResponseCode;
import com.tangkeyon.dao.ShippingMapper;
import com.tangkeyon.pojo.Shipping;
import com.tangkeyon.service.IShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author: tangkeyon@gmail.com
 * @Date: 2019/6/13 20:23
 * @Version 1.0
 */
@Service("iShippingService")
public class IShippingServiceImpl implements IShippingService {
    @Autowired
    private ShippingMapper shippingMapper;

    @Override
    public ResponseCode add(Integer userId, Shipping shipping) {
        if(userId==null && shipping==null)
            return ResponseCode.createByErrorMessage("内部参数错误");
        shipping.setUserId(userId);
        int resultCount = shippingMapper.insert(shipping);
        if(resultCount==0)
            return  ResponseCode.createByErrorMessage("添加收货地址失败");
//        向页面传递返回的主键
        Map map= Maps.newHashMap();
        map.put("shippingId",shipping.getId());
        return ResponseCode.creatBySuccess(map,"添加成功");
    }

    @Override
    public ResponseCode del(Integer userId, Integer shippingId) {
        if(userId==null && shippingId==null)
            return ResponseCode.createByErrorMessage("内部参数错误");
        if (!checkShippingExist(userId, shippingId))
            return ResponseCode.createByErrorMessage("该收获地址不存在");
        int resultCount = shippingMapper.deleteShippingByIdUserId(shippingId, userId);
        if(resultCount==0)
            return  ResponseCode.createByErrorMessage("删除收货地址失败");
        return ResponseCode.creatBySuccess("删除成功");
    }

    @Override
    public ResponseCode update(Integer userId, Shipping shipping) {
        if(userId==null && shipping==null)
            return ResponseCode.createByErrorMessage("内部参数错误");
        shipping.setUserId(userId);
        if (!checkShippingExist(userId, shipping.getId()))
            return ResponseCode.createByErrorMessage("该收获地址不存在");
        int resultCount = shippingMapper.updateShippingByIdShipping(shipping);
        if(resultCount==0)
            return  ResponseCode.createByErrorMessage("修改收货地址失败");
        return ResponseCode.creatBySuccess("修改收货地址成功");
    }

    @Override
    public ResponseCode<PageInfo> list(Integer userId, Integer pageSize, Integer pageNum) {
        if(userId==null){
            return ResponseCode.createByErrorMessage("内部参数错误");
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Shipping> shippingList = shippingMapper.selectAllShippingByUserId(userId);
        if(shippingList.size()==0){
            return ResponseCode.createByErrorMessage("没有创建收货地址");
        }
        PageInfo pageInfo=new PageInfo(shippingList);
        return ResponseCode.creatBySuccess(pageInfo);
    }

    @Override
    public ResponseCode select(Integer userId, Integer shippingId) {
        if(userId==null && shippingId==null)
            return ResponseCode.createByErrorMessage("内部参数错误");
        Shipping shipping = shippingMapper.selectShippingByIdUSerId(shippingId, userId);
        if(shipping==null)
            return ResponseCode.createByErrorMessage("获取地址详情错误");

        return ResponseCode.creatBySuccess(shipping);
    }

    private boolean checkShippingExist(Integer userId, Integer shippingId) {
        Shipping shipping = shippingMapper.selectShippingByIdUSerId(shippingId, userId);
        if(shipping==null)
            return false;
        return true;
    }
}
